package com.eurobcoin.balance;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.ibatis.session.SqlSession;

import com.eurobcoin.dao.mapper.TPCurrencyMapper;
import com.eurobcoin.dao.mapper.UserBalanceMapper;
import com.eurobcoin.dao.model.TPCurrency;
import com.eurobcoin.dao.model.TPCurrencyExample;
import com.eurobcoin.dao.model.UserBalance;
import com.eurobcoin.dao.model.UserBalanceExample;

public class UserBalanceManager {

	public int getCurrencyDecimals(SqlSession sqlSession, int currencyId) {
		
		int ret = 0;
		
		TPCurrencyMapper currencyMapper = sqlSession.getMapper(TPCurrencyMapper.class);
		TPCurrencyExample example = new TPCurrencyExample();
		TPCurrencyExample.Criteria criteria = example.createCriteria();
		criteria.andCur_idEqualTo(currencyId);
		
		List<TPCurrency> currenciesList = currencyMapper.selectByExample(example);
		if(currenciesList.size() != 0)
			ret = currenciesList.get(0).getCur_decimals();
		
		return ret;
	}
	
	public void updateUserBalance(
			SqlSession sqlSession, 
			int userId, 
			int currency, 
			long newBalance) {
		
		UserBalanceMapper userBalanceMapper = sqlSession.getMapper(UserBalanceMapper.class);
		
		UserBalanceExample userBalanceExample = new UserBalanceExample();
		UserBalanceExample.Criteria criteria = userBalanceExample.createCriteria();
		criteria.andCur_idEqualTo(currency);
		criteria.andUsr_idEqualTo(userId);
		
		UserBalance userBalance = new UserBalance();
		userBalance.setCur_id(currency);
		userBalance.setUba_balance(newBalance);
		userBalance.setUsr_id(userId);
		
		int count = userBalanceMapper.countByExample(userBalanceExample);
		if(count == 0) {
			userBalanceMapper.insert(userBalance);
		} else {
			userBalanceMapper.updateByExample(userBalance, userBalanceExample);
		}
	}
	
	public UserBalanceModel getUserBalanceByCurrencyId(
			SqlSession sqlSession, 
			int userId, 
			int currencyId, 
			Locale locale) {
		
		UserBalanceModel ret = new UserBalanceModel();
		
		UserBalanceMapper userBalanceMapper = sqlSession.getMapper(UserBalanceMapper.class);
		TPCurrencyMapper currencyMapper = sqlSession.getMapper(TPCurrencyMapper.class);
		
		UserBalanceExample userBalanceExample = new UserBalanceExample();
		UserBalanceExample.Criteria criteria = userBalanceExample.createCriteria();
		criteria.andCur_idEqualTo(currencyId);
		criteria.andUsr_idEqualTo(userId);
		
		List<UserBalance> userBalanceList = userBalanceMapper.selectByExample(userBalanceExample);
		if(userBalanceList.size() > 0) {
			ret.setBalance(userBalanceList.get(0).getUba_balance());
		}
		
		ret.setLocale(locale);
		
		TPCurrency tpCurrency = currencyMapper.selectByPrimaryKey(currencyId);
		
		ret.setCurrencyCode(tpCurrency.getCur_code());
		ret.setCurrencyId(currencyId);
		ret.setDecimals(tpCurrency.getCur_decimals());
		
		return ret;
	}
	
	public List<UserBalanceModel> getUserBalanceById(SqlSession sqlSession, int userId, Locale locale) {
		
		List<UserBalanceModel> ret = new ArrayList<UserBalanceModel>();
		
		UserBalanceMapper userBalanceMapper = sqlSession.getMapper(UserBalanceMapper.class);
		TPCurrencyMapper currencyMapper = sqlSession.getMapper(TPCurrencyMapper.class);
		
		TPCurrencyExample example = new TPCurrencyExample();
		example.setOrderByClause("cur_order ASC");
		
		List<TPCurrency> currenciesList = currencyMapper.selectByExample(example);
		
		for(TPCurrency currency : currenciesList) {
			UserBalanceExample userBalanceExample = new UserBalanceExample();
			UserBalanceExample.Criteria criteria = userBalanceExample.createCriteria();
			criteria.andCur_idEqualTo(currency.getCur_id());
			criteria.andUsr_idEqualTo(userId);
			
			UserBalanceModel userBalanceModel = new UserBalanceModel();
			userBalanceModel.setCurrencyCode(currency.getCur_code());
			userBalanceModel.setCurrencyId(currency.getCur_id());
			userBalanceModel.setDecimals(currency.getCur_decimals());
			userBalanceModel.setLocale(locale);
			
			List<UserBalance> userBalanceList = userBalanceMapper.selectByExample(userBalanceExample);
			if(userBalanceList.size() == 0) {
				userBalanceModel.setBalance(0);
			} else {
				userBalanceModel.setBalance(userBalanceList.get(0).getUba_balance());
			}
			
			ret.add(userBalanceModel);
		}
		
		return ret;
		
	}
	
}
